﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentScheduler;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.ProcessConsole
{
    /// <summary>
    /// 极光消息推送
    /// </summary>
    public class PushJob : BaseJob
    {
        public override void DoAction()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //全局的系统消息不要自动发送
            var list = dbcontext.ADZ_Pushmessage.Where(t => t.MsgStatus == 0 && t.IsShow==1 && (t.Sender == null || t.Sender == "" || t.Sender == "system")).ToList();
            foreach (var item in list)
            {
                PushMessageRequest request = PushMsgBiz.Instance.GetPushMsgReq(dbcontext, item);
                if (request != null)
                {
                    try
                    {
                        JpushBiz.PushMsg(request);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error($"推送消息异常【{request.Text}】：" + ex.GetExcetionMessage());
                    }
                    finally
                    {
                        //推送成功，更新相关信息
                        item.PushTime = DateTime.Now;
                        item.MsgStatus = 10;
                        dbcontext.SaveChanges();
                    }
                }
                else
                {
                    //推送成功，更新相关信息
                    item.PushTime = DateTime.Now;
                    item.MsgStatus = 11;
                    dbcontext.SaveChanges();
                }
            }
        }
    }
}
